<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>简单工厂</title>
</head>
<body>
<script>
    //抽象的产品类
    class Calculation{
        toDo(x,y){
            console.log(`亲，你算什么东西?这可不是骂人啊`)
        }
    }

    //具体的产品
    class Add extends Calculation{
        toDo(x,y){
            console.log(`我要算加法:${x}+${y}=${x+y}`)
        }
    }

    class Minus extends Calculation{
        toDo(x,y){
            console.log(`我要算减法:${x}-${y}=${x-y}`)
        }
    }

    class Multiply extends Calculation{
        toDo(x,y){
            console.log(`我要算乘法:${x}*${y}=${x*y}`)
        }
    }

    class Divide extends Calculation{
        toDo(x, y) {
            console.log(`我要算除法:${x}/${y}=${x/y}`)
        }
    }

    //工厂
    class CalculationFactory{
        static getResult(name){
            switch (name){
                case "+":
                    return new Add()
                    break
                case "-":
                    return new Minus()
                    break
                case "*":
                    return new Multiply()
                    break
                case "/":
                    return new Divide()
                    break
                default:
                    throw new Error("请不要难为我游戏机")
                    break
            }
        }
    }

    CalculationFactory.getResult("+").toDo(6,6)
    CalculationFactory.getResult("-").toDo(6,6)
    CalculationFactory.getResult("*").toDo(6,6)
    CalculationFactory.getResult("/").toDo(6,6)

</script>
</body>
</html>